package 网络编程.UDP协议.网络聊天室;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class 发送数据 {
    public static void main(String[] args) throws IOException {
        Scanner sc=new Scanner(System.in);
        //1.创建发送数据的DatagramSocket类
        DatagramSocket ds=new DatagramSocket();

        //2.对要发送的数据进行打包
        //创建接收消息的主机
        InetAddress address=InetAddress.getByName("127.0.0.1");
        //发送消息的端口
        int port = 5564;
        while (true){
            System.out.print("请输入要发送的消息:");
            String str = sc.next();
            if (str.equals("100")){
                for (int i = 0; i < 100; i++) {
                    byte[] b = String.valueOf(i).getBytes();
                    DatagramPacket dp=new DatagramPacket(b,b.length,address,port);
                    //发送
                    ds.send(dp);
                }
            }
            //把输入的消息转化为字节数组
            byte[] bytes = str.getBytes();
            //进行打包
            DatagramPacket dp=new DatagramPacket(bytes,bytes.length,address,port);

            //3.发送数据
            ds.send(dp);
            if (str.equals("886")){
                break;
            }

        }
        //4.释放资源
        ds.close();
    }
}
